package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.Income;
import com.ctgu.fcftsystem.pojo.Sercurity_trade;
import com.ctgu.fcftsystem.pojo.Sharesholder;
import com.ctgu.fcftsystem.pojo.Spend;
import com.ctgu.fcftsystem.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/Sercurity_tradeController")
public class Sercurity_tradeController {
    @Autowired
    private Sercurity_tradeService sercurity_tradeService;
    @Autowired
    private  IncomeService incomeService;
    @Autowired
    private  SpendService spendService;

    @RequestMapping("/findByPaging")
    public Object findByPaging(int pageCount,int pageSize){
        ModelMap map = new ModelMap();
        PageHelper.startPage(pageCount, pageSize);//设置数据库分页查询的范围
        try {
            List<Sercurity_trade> userList = sercurity_tradeService.findByPaging(pageCount, pageSize);
            //分页的信息
            PageInfo<Sercurity_trade> pageInfo=new PageInfo<>(userList);
            map.put("msg","查询所有角色成功");
            map.put("data",pageInfo);
            map.put("code",0);
        }catch (Exception e){
            e.getStackTrace();
            map.put("msg","查询所有角色失败");
            map.put("code",1);
        }
        return map;
    }

    /**
     * 添加证券交易信息
     *
     * @param noteTaker
     * @param tradeType
     * @param tradePartner
     * @param price
     * @param number
     * @param money
     * @param time
     * @return
     */
    @RequestMapping("/addSercurity_tradeInformation")
    public Map addSercurity_tradeInformation(String noteTaker, String tradeType, String tradePartner
            , Double price, Integer number, Double money, String time, String tradeWho) {
        Map<String, Object> map = new HashMap<>();
        int flag;
        if (noteTaker != null && tradeType != null && tradePartner != null && (price != null && price > 0) && (number != null && number > 0)
                && (money != null && money > 0) && time != null) {

            Sercurity_trade sercurity_trade = new Sercurity_trade();
            sercurity_trade.setNoteTaker(noteTaker);
            sercurity_trade.setTradeType(tradeType);
            sercurity_trade.setTradePartner(tradePartner);
            sercurity_trade.setPrice(price);
            sercurity_trade.setNumber(number);
            sercurity_trade.setMoney(money);
            sercurity_trade.setTime(time);
            if(tradeType.equals("买进")) {
                Income income = new Income();
                income.setIncomeNum((int) Math.round(money));
                income.setIncomeType(2);
                income.setIncomeWho(tradeWho);
                income.setIncomeDate(new Date());
                income.setIncomePath("股票");
                income.setIncomeRemark(price+"*"+number+"="+money);
                int a = incomeService.addIncome(income);
                System.out.println("添加收入记录成功" + a);
            }else {
                Spend spend = new Spend();
                spend.setSpendNum((int) Math.round(money));
                spend.setSpendType(4);
                spend.setSpendWho(tradeWho);
                spend.setSpendDate(new Date());
                spend.setSpendPath("股票");
                spend.setSpendRemark(price+"*"+number+"="+money);
                int f = spendService.addSpend(spend);
                System.out.println("添加支出记录成功" + f);
            }

            flag = sercurity_tradeService.addSercurity_tradeInformation(sercurity_trade);
        } else {
            flag = -1;
        }
        if (flag > 0) {
            map.put("code", 0);
            map.put("msg", "添加证券交易信息成功");
            map.put("data", flag);

        } else {

            map.put("code", 1);
            map.put("msg", "添加证券交易信息失败");
            map.put("data", flag);
        }

        return map;


    }

    /**
     * 根据记录人、股票的交易类型、时间等多重条件进行动态查询证券流水账记录
     * @param noteTaker
     * @param tradeType
     * @param time
     * @return
     */
    @RequestMapping("/searchsercurity_tradelist")
    public Map searchsercurity_tradelist(String noteTaker, String tradeType, String time) {
        Map<String, Object> map = new HashMap<>();
        Sercurity_trade sercurity_trade = new Sercurity_trade();
        sercurity_trade.setNoteTaker(noteTaker);
        sercurity_trade.setTradeType(tradeType);
        sercurity_trade.setTime(time);
        List<Sercurity_trade> sercurity_tradeList = sercurity_tradeService.searchsercurity_tradelist(sercurity_trade);
        if (sercurity_tradeList != null && sercurity_tradeList.size() > 0) {
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", sercurity_tradeList);
        } else {
            map.put("code", 1);
            map.put("msg", "查询失败");
            map.put("data", -1);
        }
        return map;

    }

    /**
     * 根据tradeId删除证券交易信息
     * @param tradeId
     * @return
     */
    @RequestMapping("/deleteSercurity_tradeInformation")
    public Map deleteSercurity_tradeInformation(Integer tradeId){
        Map<String,Object> map = new HashMap<>();
        int flag;
        if(tradeId!=null&&tradeId>0){
            flag = sercurity_tradeService.deleteSercurity_tradeInformation(tradeId);

        }else{
            flag=-1;
        }

        if(flag==1){
            map.put("code",0);
            map.put("msg","删除成功");
            map.put("data",flag);
        }else{
            map.put("code",1);
            map.put("msg","删除失败");
            map.put("data",flag);
        }
        return map;
    }

    /**
     * 维护证券流水账信息
     * @param tradeId
     * @param noteTaker
     * @param tradeType
     * @param tradePartner
     * @param price
     * @param number
     * @param money
     * @param time
     * @return
     */
    @RequestMapping("/updateSercurity_tradeInformation")
    public Map updateSercurity_tradeInformation(Integer tradeId, String noteTaker, String tradeType, String tradePartner,
                                                Double price, Integer number,Double money,String time)
    {
                Map<String, Object> map = new HashMap<>();
                int flag;
                if ((tradeId!=null&&tradeId>0)&& (noteTaker != null || tradeType != null || tradePartner != null || (price != null && price > 0) ||
                        (number != null && number > 0) || (money != null && money > 0) || time != null)){

                        Sercurity_trade sercurity_trade = new Sercurity_trade();
                        sercurity_trade.setTradeId(tradeId);
                        sercurity_trade.setNoteTaker(noteTaker);
                        sercurity_trade.setTradeType(tradeType);
                        sercurity_trade.setTradePartner(tradePartner);
                        sercurity_trade.setPrice(price);
                        sercurity_trade.setNumber(number);
                        sercurity_trade.setMoney(money);
                        sercurity_trade.setTime(time);
                        flag = sercurity_tradeService.updateSercurity_tradeInformation(sercurity_trade);
                 }else{
                    flag = -1;
                }


                if(flag>0){
                    map.put("code",0);
                    map.put("msg","维护成功");
                    map.put("data",flag);
                }else{
                    map.put("code",1);
                    map.put("msg","维护失败");
                    map.put("data",flag);
                }

        return map;
    }
}